// ml:run = $bin < input
#include <iostream>

int h1, h2, m1, m2;

int main()
{
    std::ios_base::sync_with_stdio(false);
    int n; std::cin >> n;
    char ch;
    std::cin >> ch; h1 = ch - '0';
    std::cin >> ch; h2 = ch - '0';
    std::cin >> ch;
    std::cin >> ch; m1 = ch - '0';
    std::cin >> ch; m2 = ch - '0';

    if (n == 12) {
        if (h1 == 0) {
            if (h2 == 0) h2 = 1;
        } else if (h1 == 1) {
            if (h2 > 2) h2 = 2;
        } else {
            if (h2 != 0) h1 = 0;
            else h1 = 1;
        }
        if (m1 >= 6) m1 = 5;
    } else {
        if (h1 == 2) {
            if (h2 > 3) h2 = 0;
        } else if (h1 > 2) {
            h1 = 1;
        }
        if (m1 >= 6) m1 = 5;
    }
    std::cout << h1 << h2 << ":" << m1 << m2 << "\n";
}

